﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.IncomingDocuments;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Linq;
using System.Web.UI.WebControls;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class IncomingDocuments : BaseApplicationPage, IIncomingDocumentsView
    {

        #region fields

        IncomingDocumentsPresenter _presenter;

        #endregion

        #region Properties

        public IEnumerable<RadicadoEntrante> AllIncomingDocuments { get; set; }

        #endregion

        #region form events

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new IncomingDocumentsPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void gvIncomingDocs_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType != System.Web.UI.WebControls.DataControlRowType.DataRow) return;

                RadicadoEntrante currentRadicado = e.Row.DataItem as RadicadoEntrante;
                if(currentRadicado == null) return;

                HyperLink currentCaptureLink = e.Row.FindControl("lnkCapture") as HyperLink;
                if (currentCaptureLink == null) return;

                currentCaptureLink.NavigateUrl = string.Format("javascript:openDialogAutoSizeRefreshParent('/sites/docs/_layouts/MS.InformationManagement.WP/ScanningManager.aspx?SelectedId={0}&RadicadoId={1}&currentWeb={2}')", currentRadicado.Id.Value, currentRadicado.RadicadoEntrante0, SPContext.Current.Site.Url);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            gvIncomingDocs.DataSource = AllIncomingDocuments.ToList();
            gvIncomingDocs.DataBind();
        }

        #endregion


    }
}
